package com.ctshk.task.worker.processors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.dto.Result;
import com.ctshk.rpc.custom.dto.inquiry.InquiryInfoDTO;
import com.ctshk.rpc.custom.service.ICustomizedInquiryService;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description 延迟加载demo,如创建订单后10分钟内未付款则将当前订单置为过期状态
 * @author 谢诗宏
 * @date 2021/2/18
 */
@Slf4j
@Component
public class LazyProcessorDemo implements BasicProcessor {

    @DubboReference
    private ICustomizedInquiryService inquiryService;

    /**
     * 进行具体的设置超时操作
     * @param context   请求参数,如订单id,传入json格式,通过context.getInstanceParams()参数接收
     * @return
     * @throws Exception
     */
    @Override
    public ProcessResult process(TaskContext context) throws Exception {

        OmsLogger omsLogger = context.getOmsLogger();
        JSONObject jsonObject = JSON.parseObject(context.getInstanceParams());
        Result result = inquiryService.expires(jsonObject.getLong("id"));
        if (!result.isSuccess()) {
            omsLogger.error("设置订单超时失败");
        } else {
            omsLogger.info("设置订单超时成功");
        }
        // 根据控制台参数判断是否成功
        boolean success = !"failed".equals(context.getJobParams());
        omsLogger.info("InquiryExpiresProcessor finished process,success: .", success);

        return new ProcessResult(success, context + ": " + success);
    }

}
